import mx.controls.NumericStepper;
import mx.utils.Delegate;
import com.dmotiko.sathya.*
import com.dmotiko.general.*
import com.dmotiko.util.*

class PhotoShower
extends MovieLoaderClip {
	
	public static var PHOTO_CLOSED:String = "photoshower_closed";
	public static var PREV_PHOTO:String = "photoshower_prevPhoto";
	public static var NEXT_PHOTO:String = "photoshower_nextPhoto";
	
	private var interpolator:MovieClip;
	private var mcPrev:MovieClip;
	private var mcSepPrev:MovieClip;
	private var mcNext:MovieClip;
	private var mcSepNext:MovieClip;
	private var txtLabel:TextField;
	private var tf:TextFormat;
	private var tf2:TextFormat;
	
	function PhotoShower(){
		super();
		MovieEffects.fadeIn();
		tf = new TextFormat();
		tf2 = new TextFormat();
		tf.color = 0x722F62;
		tf2.color = 0xC94B8D;
		txtLabel.autoSize = "left";
		
		txtLabel.text = "";
		centerContent();
		
		var root:PhotoShower = this;
		
		mcPrev.onRelease = function(){
			root.dispatchEvent( { target: root, type: PhotoShower.PREV_PHOTO } );
		}
		mcNext.onRelease = function(){
			root.dispatchEvent( { target: root, type: PhotoShower.NEXT_PHOTO } );
		}
		
		this._alpha = 0;
		this._visible = false;
	}
		
	public function setData(data:Object){
		super.setData(data);
		var sTitle:String = data.title;
		var sDescription:String = data.description;
		var sText:String = sTitle + " _ " + sDescription;
		var root:PhotoShower = this;
				
		var textWriter:MovieClip = DummyFactory.getNextDummy(this);
		textWriter.nCount = 0;
		textWriter.onEnterFrame = function(){
			if(this.nCount >= sText.length){
				delete this.onEnterFrame;
				this.removeMovieClip();
			} else {
				var n = Math.round( (sText.length - this.nCount) / 5 );
				if(n < 1) n = 1;
				this.nCount+= n;
			}
			root.txtLabel.text = sText.substring(0, this.nCount);
			root.txtLabel.setTextFormat(0, sTitle.length, tf);
			root.txtLabel.setTextFormat(sTitle.length+1, sTitle.length+1+sDescription.length, tf2);
			root.centerContent();
		}
		
		interpolator.remove();
		loadContent(data.big);
	}
	
	public function show(){
		this._visible = true;
		this["fadeIn"]();
	}
	
	public function hide(){
		this["fadeOut"]( { bind: this, listener: hideEnd } );
		dispatchEvent( { target: this, type: PhotoShower.PHOTO_CLOSED, data: "hide"} );
	}
	
	private function hideEnd(){
		this._visible = false;
		
	}
	
	private function centerContent(){
		mcSepNext._x = txtLabel._x + txtLabel._width + 3;
		mcNext._x = mcSepNext._x + mcSepNext._width + mcNext._width + 3;
		var nTotal:Number = mcNext._x + mcNext._width - mcPrev._x;
		var nX:Number = ( this._width - nTotal ) /2;
		mcPrev._x = nX;
		mcSepPrev._x = mcPrev._x + mcPrev._width + 3;
		txtLabel._x = mcSepPrev._x + mcSepPrev._width + 3;
		mcSepNext._x = txtLabel._x + txtLabel._width + 3;
		mcNext._x = mcSepNext._x + mcSepNext._width + mcNext._width + 3;
	}
	
	public function onLoadInit(target:MovieClip){
		super.onLoadInit();
		target._alpha = 0;
		target._x = 6;
		target._y = 6;
		interpolator = target.fadeIn( { listener: fadeEnd});
		
		var root:PhotoShower = this;
		function fadeEnd(){
			target.onRelease = function(){
				root.hide();
			}
		}
	}
	
}